﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp_core
{
    /// <summary>
    /// 是否继承之
    /// </summary>
    public class IsAssignable
    {
        public void Show()
        {
            Type a = typeof(A);
            Type b = typeof(B);
            //是否继承关系。有点难记。
            Console.WriteLine(a.IsAssignableFrom(b));       //true a.是不是(b)的父类
            Console.WriteLine(b.IsAssignableFrom(a));       //false 

            //看来.net core已经去除了这个IsAssignableTo
            //Console.WriteLine(a.IsAssignableTo(b));       //true  a.是不是(b)的子类
            //Console.WriteLine(b.IsAssignableTo(a));       //false
        }

        public class A { };
        public class B : A { };


        /// <summary>
        /// String 的比较
        /// </summary>
        public void Show2()
        {
            var names = new List<string> { "Bruke", "MaCy", "lucY" };
            var sigers = new List<string> { "macy", "LucY" };

            var names2 = names.Where(w => sigers.Contains(w)).ToList();//完全匹配
            var names3 = names.Where(w => sigers.Contains(w,new StrEqualityCompare())).ToList();//不区分大小写匹配  
            //.net framework 这里可以使用func 。.net core 只能使用IEqualityCompare
        }

        /// <summary>
        /// String不区分大小写比较器
        /// </summary>
        private class StrEqualityCompare : IEqualityComparer<string>
        {
            public bool Equals(string x, string y)
            {
                if (Object.ReferenceEquals(x, y))
                    return true;
                if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                    return false;
                return x.Equals(y, StringComparison.OrdinalIgnoreCase);
            }
            
            public int GetHashCode(string obj)
            {
                return base.GetHashCode();
            }
        }

        /// <summary>
        /// string 排序
        /// </summary>
        public void Show3()
        {
            string a = "a";
            string A = "A";
            string b = "b";
            string B = "B";
            string a1 = "a1";
            string a2 = "a2";

            //值越小，排名越大。
            //数字<<大小字母<<小写字母
            //CompareOrdinal(strA,strB)= strA - strB 。
            var aA = string.CompareOrdinal(a, A);//+
            var ab = string.CompareOrdinal(a, b);//+
            var aB = string.CompareOrdinal(a, B);//+
            var a1b1 = string.CompareOrdinal(a1, a2);//-
            var test1 = string.CompareOrdinal("a", "1");//+1
            var test2 = string.CompareOrdinal("A", "1");//+1
        }

    }
}
